package com.jxud.controller;

import com.jxud.Pojo.Result;
import com.jxud.Pojo.Users;
import com.jxud.enums.UserStatus;

import com.jxud.service.UsersService;
import com.jxud.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin(origins = "*")
// 登录
public class LoginController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private PasswordEncoder  passwordEncoder;

    @PostMapping("/api/auth/login")
    public Result login(@RequestBody Users users) {
        log.info("========================================用户登录请求=================================================");
        Users user = usersService.login(users);
        if (user == null) {
            return Result.error("未查询到账号信息，账号名称不正确");
        }
        log.info("登录的账号{}", user.getUsername());

        // 注意：参数顺序是 (明文密码, 加密后的密码)
        if (!passwordEncoder.matches(users.getPassword(), user.getPassword())) {
            return Result.error("账号名称或密码不正确");
        }
        if (user.getStatus() != UserStatus.ENABLED) {
            return Result.error("您的账号已经被限制");
        }

        // 生成 JWT
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("wxName", user.getWechatNickname());
        String jwt = JwtUtils.generateJwt(claims);

        return Result.success(jwt);
    }



    @PostMapping("/api/auth/register")
    public Result registerUser(@RequestBody Users users){
        log.info("========================================用户注册请求=================================================");
        if (users.getUsername() == null || users.getUsername().equals("")) {
            return Result.error("账号和密码不呢为空");
        }
        Users Username = usersService.login(users);
        if (Username == null) {
            usersService.registerUser(users);
            return Result.success("注册成功");
        }else  {
            return Result.error("用户名称已存在");
        }
    }

    @PostMapping("/api/getUser")
    public Result getUser(@RequestBody Users users, HttpServletRequest request){

        log.info("管理员获取所有用户");
        List<Users> user = usersService.getUser(users,request);

        if (user == null) {
            return Result.error("您没有操作权限");
        }

        // 创建一个列表，用于存放每个用户的Map
        List<Map<String, Object>> result = new ArrayList<>();
        for (Users u : user) {
            Map<String, Object> userMap = new HashMap<>(); // 每个用户一个独立的Map
            if (u.getUsername().equals("admin")){
                continue;
            }
            userMap.put("id", u.getId());
            userMap.put("name", u.getUsername());
            result.add(userMap); // 将当前用户的Map加入列表
        }
        return Result.success(result);
    }


}
